/*
package cloud.xlh.my_system.aspect;

import cloud.xlh.infrastructure.db.dynamic_dataSource_change.AbstractDynamicRoutingDataSourceAspect;
import cloud.xlh.infrastructure.enums.DataSourceEnum;
import cloud.xlh.infrastructure.utils.StringUtil;
import cloud.xlh.my_system.config.DynamicDataSourceContextHolder;
import cloud.xlh.my_system.annotation.DataSource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;


*/
/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/3/26 18:47
 * @ Description:
 *//*

@Aspect
@Component
public class DynamicRoutingDataSourceAspect implements AbstractDynamicRoutingDataSourceAspect {

    @Override
    @Pointcut("@annotation(cloud.xlh.my_system.annotation.DataSource)||@within(cloud.xlh.my_system.annotation.DataSource)")
    public void dynamicRoutingDataSourcePointCut() {
    }

    @Override
    @Around("dynamicRoutingDataSourcePointCut()")
    public Object useCustomDataSource(ProceedingJoinPoint pjp) {
        AbstractDynamicRoutingDataSourceAspect.super.useCustomDataSource(pjp);
        return ExecuteStrategy(pjp);
    }

    @Override
    public Object ExecuteStrategy(ProceedingJoinPoint pjp) {
        // 获取注解中数据源名称
        // 1.获取当前方法
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        // 2.获取当前方法上@DataSource的值，若方法上不存在，查找类上的值
        DataSource dataSource = getDataSource(methodSignature);
        // 3.若注解不为空，获取值，并存入本地线程
        String value = "";
        if (dataSource != null) {
            value = dataSource.value();
            //将值存入ThreadLocal中
            DynamicDataSourceContextHolder.setDataSourceKeys(value);
        }
        if (StringUtil.isEmpty(value)) {
            value = DataSourceEnum.MASTER.getDesc();
            DynamicDataSourceContextHolder.setDataSourceKeys(value);
        }
        //4.执行原方法,有返回值直接返回
        try {
            return pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            boolean containDataSourceKey = DynamicDataSourceContextHolder.containDataSourceKey(value);
            if (containDataSourceKey) {
                //5.移除本地线程的值
                DynamicDataSourceContextHolder.clearDataSourceKey();
            }
        }
        return null;
    }

    private DataSource getDataSource(MethodSignature methodSignature) {
        //查找方法上的@DataSource注解 AnnotationUtils.findAnnotation(Method,annotationType)
        DataSource annotation = AnnotationUtils.findAnnotation(methodSignature.getMethod(), DataSource.class);
        if (annotation != null) {//方法上有值，直接返回
            return annotation;
        }
        //如果方法中没有@DataSource注解，查找类上的@DataSource注解 AnnotationUtils.findAnnotation(Class,annotationType)
        return AnnotationUtils.findAnnotation(methodSignature.getDeclaringType(), DataSource.class);

    }

}
*/
